package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.VoiceTask;
import com.tbit.uqbike.object.pojo.dto.TaskDTO;
import com.tbit.uqbike.object.pojo.dto.VoiceTaskDTO;
import com.tbit.uqbike.object.pojo.vo.VoiceTaskVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.VoiceTaskService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * 语音任务controller
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2024/3/15
 */
@RestController
@RequestMapping("/voiceTask")
public class VoiceTaskController {

    @Resource
    private VoiceTaskService voiceTaskService;


    /**
     * 分页查询语音任务列表
     *
     * @param voiceTaskDTO voiceTaskDTO
     * @return Result<PageResult < VoiceTaskVO>>
     */
    @GetMapping("/queryPage")
    public Result<PageResult<VoiceTaskVO>> queryPage(@Valid VoiceTaskDTO voiceTaskDTO) {
        return voiceTaskService.queryPage(voiceTaskDTO);
    }

    /**
     * 新增语音任务
     *
     * @param voiceTask voiceTask
     * @return Result<Object>
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.VOICE_CUSTOM, methods = MethodEnum.VOICE_CUSTOM_TASK_ADD)
    public Result<Object> add(@RequestBody VoiceTask voiceTask, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        if (Objects.isNull(voiceTask.getTaskName())) {
            return Result.error("请输入任务名称");
        }
        if (Objects.isNull(voiceTask.getId())) {
            return Result.error("请选择语音");
        }
        return voiceTaskService.add(voiceTask, login);
    }

    /**
     * 编辑语音任务
     *
     * @param dto dto
     * @return Result<Object>
     */
    @PutMapping("/update")
    @OperateLog(module = ModuleEnum.VOICE_CUSTOM, methods = MethodEnum.VOICE_CUSTOM_TASK_UPDATE)
    public Result<Object> update(@RequestBody TaskDTO dto) {
        if (Objects.isNull(dto.getVoiceTaskId())) {
            return Result.error("请选择语音任务");
        }
        if (Objects.isNull(dto.getTaskName())) {
            return Result.error("请输入任务名称");
        }
        return voiceTaskService.update(dto);
    }

    /**
     * 立即更新配置到设备
     *
     * @return Result
     */
    @GetMapping("/updateNow")
    public Result<Object> updateNow(Integer brandId, Integer accountId) {
        LoginInfo login = BaseController.getLogin(ServletUtils.getRequest());
        return voiceTaskService.updateNow(brandId, accountId, login);
    }

    /**
     * 更新单个设备
     * @param brandId 品牌id
     * @param accountId 区域id
     * @param machineNo 设备编号
     * @return result
     */
    @GetMapping("/updateOne")
    public Result<Object> updateOne(Integer brandId, Integer accountId, String machineNo){
        LoginInfo login = BaseController.getLogin(ServletUtils.getRequest());
        return voiceTaskService.updateOne(brandId, accountId, machineNo, login);
    }
}
